package com.tianyang.yukuaifuzs.business.main.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.github.gzuliyujiang.wheelpicker.contract.OnTimeSelectedListener;
import com.github.gzuliyujiang.wheelpicker.contract.TimeFormatter;
import com.github.gzuliyujiang.wheelpicker.entity.TimeEntity;
import com.github.gzuliyujiang.wheelpicker.widget.TimeWheelLayout;
import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.base.BaseTitleActivity;
import com.tianyang.yukuaifuzs.business.main.adapter.SettleManageAdapter;
import com.tianyang.yukuaifuzs.business.main.bean.SettleOutput;
import com.tianyang.yukuaifuzs.business.main.contract.SettleManageContract;
import com.tianyang.yukuaifuzs.business.main.presenter.SettleManagePresenter;
import com.tianyang.yukuaifuzs.utils.DateUtils;
import com.tianyang.yukuaifuzs.utils.ToastUtil;

import java.util.List;

import butterknife.BindView;

/**
 * Description：结算管理
 * Created by xyj on 2024/11/19
 */
public class SettleManageActivity extends BaseTitleActivity implements SettleManageContract.View {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.timeTitleLl)
    LinearLayout timeTitleLl;
    @BindView(R.id.timeWheelLayout)
    TimeWheelLayout timeWheelLayout;
    @BindView(R.id.confirmTv)
    TextView confirmTv;

    private SettleManageAdapter vSettleManageAdapter;

    private SettleManageContract.Presenter mPresenter;

    private int selectHour = 0, selectMinute = 0;

    private SettleOutput mSelectSettle;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_settle_manage;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("结算管理");
        timeWheelLayout.setResetWhenLinkage(false);
        timeWheelLayout.setTimeFormatter(new TimeFormatter() {
            @Override
            public String formatHour(int hour) {
                return hour + " 时";
            }

            @Override
            public String formatMinute(int minute) {
                return minute + " 分";
            }

            @Override
            public String formatSecond(int second) {
                return second + " 秒";
            }
        });
        timeWheelLayout.setDefaultValue(TimeEntity.target(selectHour, selectMinute, 0));

        vSettleManageAdapter = new SettleManageAdapter();
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(vSettleManageAdapter);
        mPresenter = new SettleManagePresenter(this);
        mPresenter.getSettleData();
    }

    @Override
    protected void setListener() {
        super.setListener();
        timeWheelLayout.setOnTimeSelectedListener(new OnTimeSelectedListener() {
            @Override
            public void onTimeSelected(int hour, int minute, int second) {
                selectHour = hour;
                selectMinute = minute;
            }
        });
        confirmTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSelectSettle == null) {
                    ToastUtil.show("请选择结算周期");
                    return;
                }
                if (TextUtils.equals("4", mSelectSettle.getCode())) {
                    String auditTime = DateUtils.timeToString(selectHour, selectMinute);
                    mPresenter.updateMemberSettleCycle(mSelectSettle.getCode(), auditTime);
                } else {
                    mPresenter.updateMemberSettleCycle(mSelectSettle.getCode(), null);
                }
            }
        });
        vSettleManageAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mSelectSettle = vSettleManageAdapter.getItem(position);
                vSettleManageAdapter.setChooseIndex(position);
                if (TextUtils.equals("4", mSelectSettle.getCode())) {
                    timeTitleLl.setVisibility(View.VISIBLE);
                    timeWheelLayout.setVisibility(View.VISIBLE);
                } else {
                    timeTitleLl.setVisibility(View.GONE);
                    timeWheelLayout.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    public void showSettleView(List<SettleOutput> outputs) {
        if (outputs != null && !outputs.isEmpty()) {
            vSettleManageAdapter.setNewData(outputs);
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(SettleManageContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
